# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

import functools

from coin.exchange.base.kr_rest.product import NativeProduct, NormProduct
from coin.exchange.liqui_v3.kr_rest.constants import (
    symbol_list,
    currency_list,
    active_base_currency_list,
    quote_currency_list,
)
from coin.exchange.liqui_v3.kr_rest.currency import LiquiCurrency


@functools.lru_cache(maxsize=1)
def get_active_symbol_list():
  res = []
  for base in active_base_currency_list:
    for quote in quote_currency_list:
      if base != quote:
        product = LiquiProduct.FromStrPair(base, quote)
        if product.native_symbol in symbol_list:
          res.append(product.native_symbol)
  return res


class LiquiProduct(NativeProduct):
  _exchange = "Liqui"
  NativeCurrencyType = LiquiCurrency

  def __init__(self, base, quote):
    assert base.currency in currency_list, (base, currency_list)
    assert quote.currency in quote_currency_list, quote
    assert base != quote, (base, quote)
    native_symbol = "%s_%s" % (base.native_currency, quote.native_currency)
    super().__init__(base, quote, native_symbol)

  @classmethod
  def FromStrProductImpl(cls, product, current_datetime):
    assert type(product) == str
    assert '-' in product
    pair = product.split('-')
    assert len(pair) == 2
    base, quote = pair
    base = LiquiCurrency.FromStrCurrency(base)
    quote = LiquiCurrency.FromStrCurrency(quote)
    return LiquiProduct(base, quote)

  @classmethod
  def FromStrNativeProductImpl(cls, product, current_datetime):
    product = product.lower()
    assert type(product) == str
    assert '_' in product
    assert product in symbol_list, product
    pair = product.split('_')
    assert len(pair) == 2
    base, quote = pair
    base = LiquiCurrency.FromStrNativeCurrency(base)
    quote = LiquiCurrency.FromStrNativeCurrency(quote)
    return LiquiProduct(base, quote)
